將BotFather加入對話
輸入 /newbot 的指令,再輸入欲產生的Bot名稱,將產生後token記錄下來後續使用
go get -u github.com/go-telegram-bot-api/telegram-bot-api
package main
import (
	"log"
	"github.com/go-telegram-bot-api/telegram-bot-api"
)
func main() {
	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
	if err != nil {
		log.Panic(err)
	}
	bot.Debug = true
	log.Printf("Authorized on account %s", bot.Self.UserName)
	u := tgbotapi.NewUpdate(0)
	u.Timeout = 60
	updates, err := bot.GetUpdatesChan(u)
	for update := range updates {
		if update.Message == nil { // ignore any non-Message Updates
			continue
		}
		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
		msg.ReplyToMessageID = update.Message.MessageID
		bot.Send(msg)
	}
}
https://api.telegram.org/13919:AAFQ-MrwPrEVbBfNWUQv8GX/sendMessage?chat_id=13919&text=Hello+World
API Request
https://api.telegram.org/13919:AAFQ-MrwPrEVbBfNWUQv8GX/getMe
得到接收的訊息
https://api.telegram.org/13919:AAFQ-MrwPrEVbBfNWUQv8GX/getUpdates
https://api.telegram.org/13919:AAFQ-MrwPrEVbBfNWUQv8GX/sendMessage?chat_id=13919&text=Hello+World
application/json
application/x-www-form-urlencoded (無法上傳檔案)
multipart/form-data (用來上傳檔案 )
Response (JSON 格式)
成功
ok = True
result = 回覆內容
失敗
ok = False
error_code
description = 錯誤描述
Method (無視大小寫)
getMe 等同 GeTmE
必須使用 UTF-8 編碼
保存時間 (24 hrs)
兩種獲得方式 ,兩者無法並行使用
[方式一] 設定 Webhook (有新訊息時,Telegram 將會主動告知)
需要 HTTPS (TLS 1.0) 伺服器
並且將埠開在 443, 8443, 80, 8080 其一 (就算 port 80 也要求 TLS)
setWebhook 設定伺服器
https://api.telegram.org/bot/setWebhook?url=
留白表示刪除
https://api.telegram.org/bot/setWebhook?url=
getWebhookInfo 得知設定
https://api.telegram.org/bot/getWebhookInfo
deleteWebhook 刪除設定
https://api.telegram.org/bot/deleteWebhook
[方式二] 主動輪詢
getUpdates 請求,將會回傳一個 JSON 陣列
https://api.telegram.org/bot/getUpdates
[模式一] 隱私模式 Privacy Mode (預設)
由 / 開頭的指令
對機器人 Reply 的訊息
系統訊息 (e.g., 新成員)
自己是管理員的頻道
[模式二] 關閉可收到全部訊息
私訊 @BotFather
輸入 /setprivacy 指令
選擇 Bot
點擊 Disable